/*
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <comic_book_menu.h>
#include <comic.h>
#include <string.h>
#include <Ecore.h>
#include <Ecore_Evas.h>
#include <etk/Etk.h>
#include <debug.h>

extern Ecore_Evas *ee;
extern struct comic_reader_guis *comic;

static inline int get_index_in_tree(Evas_List *list, Etk_Tree_Row *row) {
	int i = 0;
	for (i = 0;i < evas_list_count(list);i++) {
		if ((Etk_Tree_Row *)evas_list_nth(list, i) == row) {
			return i;
		}
	}
	return -1;
}

void post_action_hide (void *data) {
	Etk_Widget *obj = (Etk_Widget *)data;
	etk_widget_hide(obj);
}

static int reading_book_button_cb(Etk_Object *object, void *data) {
	struct Book_Menu *bm = (struct Book_Menu *)data;
	animator_move_object_callback(
	    etk_embed_object_get(ETK_EMBED(bm->embed)),
	    animator_square_move_cb, ANIMATOR_MAGIC,
	    0, 0 ,
	    0, -HEIGHT(),
	    &post_action_hide, bm->embed);
	return ETK_TRUE;
}


static Etk_Bool book_selected_callback(Etk_Object *object, Etk_Tree_Row *row, Etk_Event_Mouse_Up *event, void *data)  {
	struct Book_Menu *bm = (struct Book_Menu *)data;
	Evas_List *list = bm->list;
	int index;
	char *name;
	if (!comic) return ETK_FALSE;
	if (!list) return ETK_FALSE;
	index = get_index_in_tree(list, row);
	debug("index = %d\n", index);
	name = (char *)evas_list_nth(comic->list, index);
	debug("Name = '%s'\n", name);
	if (comic->book) {
		if (strcmp (comic->book->name, name)) {
			comic_book_free(comic->book);
			comic->book = NULL;
			comic->book = comic_book_new(name);
			comic_book_goto_index(comic->book, 0);
		}
	} else {
		comic->book = comic_book_new(name);
		comic_book_goto_index(comic->book, 0);
	}

	evas_object_raise(etk_embed_object_get(ETK_EMBED(bm->embed)));
	animator_move_object_callback(
	    etk_embed_object_get(ETK_EMBED(bm->embed)),
	    animator_square_move_cb, ANIMATOR_MAGIC,
	    0, 0 ,
	    0, -HEIGHT(),
	    &post_action_hide, bm->embed);
	return ETK_TRUE;
}

struct Book_Menu * book_menu_list_new(Evas_List *list) {
	struct Book_Menu * ans = (struct Book_Menu *) malloc (sizeof(struct Book_Menu));
	Evas_Object *obj;
	int i;
	char *name;
	char buf[1024];
	Etk_Tree_Row *tree_row;
	memset(ans, 0 , sizeof(struct Book_Menu));
	ans->vbox = etk_vbox_new(ETK_TRUE, 0);
	sprintf(buf,"<b><font_size=24>%s</><b>", comic->book ? rindex(comic->book->name,'/') ?  rindex(comic->book->name,'/')+1: comic->book->name : "No Book");
	ans->label = etk_button_new_with_label(buf);
	ans->embed = etk_embed_new(ecore_evas_get(ee));
	ans->tree =  etk_tree_new();
	etk_container_add(ETK_CONTAINER(ans->embed), ans->vbox);
	etk_scrolled_view_policy_set(etk_tree_scrolled_view_get(ETK_TREE(ans->tree)), ETK_POLICY_HIDE, ETK_POLICY_HIDE);
	etk_scrolled_view_dragable_set(ETK_SCROLLED_VIEW(etk_tree_scrolled_view_get(ETK_TREE(ans->tree))), ETK_TRUE);

	etk_tree_mode_set(ETK_TREE(ans->tree), ETK_TREE_MODE_LIST);
	etk_tree_multiple_select_set(ETK_TREE(ans->tree), ETK_FALSE);
	etk_tree_rows_height_set (ETK_TREE(ans->tree), 60);
	ans->col = etk_tree_col_new(ETK_TREE(ans->tree), "Book", WIDTH() / 2, 0.0);
	etk_tree_col_model_add(ans->col, etk_tree_model_text_new());
	etk_tree_build(ETK_TREE(ans->tree));

	for (i = 0;i < evas_list_count(list);i++) {
		name = (char *)evas_list_nth(list, i);
		sprintf(buf, "<b><font_size=16>%s</><b>", rindex(name, '/') ? rindex(name, '/')+1 : name);
		tree_row = etk_tree_row_append(ETK_TREE(ans->tree), NULL, ans->col, buf, NULL );
		ans->list = evas_list_append(ans->list, tree_row);
	}
	etk_signal_connect_by_code(ETK_TREE_ROW_CLICKED_SIGNAL, ETK_OBJECT(ans->tree), &book_selected_callback, ans);
	etk_box_append(ETK_BOX(ans->vbox), ans->label, ETK_BOX_START, ETK_BOX_FILL, 0);
	etk_box_append(ETK_BOX(ans->vbox), ans->tree, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 0);

	etk_widget_show_all(ans->embed);

	obj = etk_embed_object_get(ETK_EMBED(ans->embed)) ;
	evas_object_resize(obj, WIDTH(), HEIGHT());
	evas_object_move(obj, 0, -HEIGHT());

	etk_tree_thaw(ETK_TREE(ans->tree));
	etk_signal_connect_by_code(ETK_BUTTON_CLICKED_SIGNAL, ETK_OBJECT(ans->label),ETK_CALLBACK(reading_book_button_cb), ans);
	return ans;
}
